package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.Customer;
import com.lutzyt.delivery.mapper.CustomerDAO;
import com.lutzyt.delivery.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerServiceImpl extends ActionServiceImpl<CustomerDAO, Customer> implements CustomerService {

  @Autowired
  private CustomerDAO customerDAO;

  @Transactional(readOnly = true)
  @Override
  public Customer findById(Integer id) {
    final Customer customer = customerDAO.selectById(id);
    return customer;
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<Customer> findByPage(int pageNum, int pageSize) {
    return customerDAO.selectPage(new Page<>(pageNum, pageSize), null);
  }

  @Override
  public void insert(Customer customer) {
    customerDAO.insert(customer);
  }

  @Override
  public void update(Customer customer) {
    customerDAO.updateById(customer);
  }

  @Override
  public void deleteById(Integer id) {
    customerDAO.deleteById(id);
  }


  @Override
  public Integer register(String customerOpenId) {
    Customer customer = new Customer();
    customer.setHeadPortrait("#");
    customer.setUsername("~");
    customer.setCode(UUID.randomUUID().toString());
    customer.setPassword("123456");
    customer.setName("~");
    customer.setPhone("~");
    customer.setWxOpenid(customerOpenId);
    this.save(customer);
    return customer.getId();
  }
}